package com.itboye.mengwu.app.ui.aizhiwu;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnScrollChangeListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ItemEntity;
import com.itboye.inageview.ListItemAdapter;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.mymeng.ActivityGuanZhu;
import com.itboye.mengwu.app.ui.mymeng.FollowActivity;
import com.itboye.mengwu.app.ui.mymeng.PrivateLtterActivity;
import com.itboye.mengwu.app.ui.mymeng.WoDeDongTaiDetail2;
import com.itboye.mengwu.app.ui.mymeng.personsettings.ActivitySiXinXiangQing;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MyTieZiCenterBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.model.VIPPersonalBean;
import com.itboye.mengwu.data.presenter.ConfigAppPresenter;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

//import com.itboye.core.imageview.RoundImage;

public class GeRenActivity extends BaseActivity implements Observer,
		IXListViewListener {

	/** Item数据实体集合 */
	private ArrayList<ItemEntity> itemEntities;
	/** ListView对象 */
	private XListView listview;
	// private RoundImage image;
	private TextView textGuan;
	private ImageView back;
	MyTieZiCenterPresenter lunTanPresenter;
	MyTieZiCenterBean tieZiCenterBean = new MyTieZiCenterBean();
	String uid, fromUid;
	ConfigAppPresenter appPresenter;
	private VIPPersonalBean vipPersonalBean;
	RoundImageView roundImage;
	private TextView textName;
	// private TextView textVip;
	private TextView qianming;
	private TextView textGuanZhu;
	private TextView textNum;
	private TextView textFenShi;
	private TextView texttextFenShiNum;
	private TextView textSiLiao;
	int pageNo = 1;
	private LuntanTieZiBean model;
	private ListItemAdapter adapter;
	RelativeLayout top;
	ArrayList<GetAList> arrayList = new ArrayList<>();
	ImageView txt_title;
	String tag;
	ImageView imgVip;
	TieZiPresenter presenter;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.gerenkongjian;
	}

	@Override
	public void onMyCreate() {
		showProgressDialog("数据加载中，请稍后", true);
		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
			// 透明状态栏
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
		// uid = getIntent().getStringExtra("uid");
		uid = (String) SPUtils.get(GeRenActivity.this, null,
				SpPublicParams.UID, "");
		model = new LuntanTieZiBean();
		adapter = new ListItemAdapter(this, arrayList);
		listview.setAdapter(adapter);
		appPresenter = new ConfigAppPresenter(this);
		tieZiCenterBean = new MyTieZiCenterBean();
		lunTanPresenter = new MyTieZiCenterPresenter(this);
		vipPersonalBean = new VIPPersonalBean();
		model.setPage_no(pageNo + "");// (page_size);
		model.setFid("4");
		presenter = new TieZiPresenter(this);
		// presenter.guanzhu(uid, fromUid, "");

		try {
			fromUid = getIntent().getStringExtra("fromUid");
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (fromUid == null) {
			model.setUid(uid);
			appPresenter.getGuanZhu(uid, uid);
		} else {
			model.setUid(fromUid);
			appPresenter.getGuanZhu(fromUid, uid);
		}
		lunTanPresenter.getInfo(model);
		listview = (XListView) findViewById(R.id.listview);
		listview.setXListViewListener(this);
		listview.setPullLoadEnable(true);
		listview.setPullRefreshEnable(true);

		View view = LayoutInflater.from(this).inflate(
				R.layout.item_hederlistview, null);
		listview.addHeaderView(view);
		textGuanZhu = (TextView) view.findViewById(R.id.textGuanZhu);
		textGuanZhu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {// 跳转关注
				// TODO Auto-generated method stub
				if (fromUid == null) {
					Intent intent = new Intent(GeRenActivity.this,
							ActivityGuanZhu.class);
					startActivity(intent);
				}
			}
		});
		textFenShi = (TextView) view.findViewById(R.id.textFenShi);
		textFenShi.setOnClickListener(new OnClickListener() {// 跳转粉丝

					@Override
					public void onClick(View v) {
						// ByAlert.alert("点击；额");
						// TODO Auto-generated method stub
						if (fromUid == null) {
							Intent intent = new Intent(GeRenActivity.this,
									FollowActivity.class);
							startActivity(intent);
						}

					}
				});
		textGuan = (TextView) view.findViewById(R.id.textGuan);
		textName = (TextView) view.findViewById(R.id.textName);
		// textVip = (TextView) view.findViewById(R.id.textVip);
		qianming = (TextView) view.findViewById(R.id.qianming);
		textNum = (TextView) view.findViewById(R.id.textNum);
		texttextFenShiNum = (TextView) view
				.findViewById(R.id.texttextFenShiNum);
		textSiLiao = (TextView) view.findViewById(R.id.textSiLiao);
		roundImage = (RoundImageView) view.findViewById(R.id.roundImage);
		imgVip = (ImageView) view.findViewById(R.id.imgVip);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View viewgroup,
					int position, long id) {
				// TODO Auto-generated method stub
				if (position > 1) {
					Intent intent = new Intent(GeRenActivity.this,
							WoDeDongTaiDetail2.class);
					intent.putStringArrayListExtra("img",
							arrayList.get(position - 2).getImg());
					intent.putExtra("tid", arrayList.get(position - 2).getTid());
					startActivity(intent);
				} else {
					txt_title.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							finish();
						}
					});

				}

			}
		});
		// listview.setOnScrollChangedListener(new
		// PullableScrollView.OnScrollChangedListener() {
		//
		// @Override
		// public void onScrollChanged(ScrollView who, int l, int t, int oldl,
		// int oldt) {
		// // TODO Auto-generated method stub
		// if(myPager != null && myPager.getHeight() > 0 ){
		// //define it for scroll height
		// int lHeight = myPager.getHeight();
		// if(t < lHeight){//=
		// int progress = (int)(new Float(t)/new Float(lHeight) * 200);//255
		// rl_title.getBackground().setAlpha(progress);
		// }else{
		// rl_title.getBackground().setAlpha(255 - 55);
		// }
		// }
		// }
		// });
		// top.getBackground().setAlpha(0);
		// listview.setOnScrollChangeListener(new OnScrollChangeListener() {
		//
		// @Override
		// public void onScrollChange(View v, int scrollX, int scrollY,
		// int oldScrollX, int oldScrollY) {
		// // TODO Auto-generated method stub
		// Log.e("scrollX", oldScrollY+"");
		// int lHeight = listview.getHeight();
		// if(scrollX < lHeight){//=
		// int progress = (int)(new Float(scrollY)/new Float(lHeight) *
		// 200);//255
		// top.getBackground().setAlpha(progress);
		// }else{
		// top.getBackground().setAlpha(255 - 55);
		// }
		// }
		// });
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (fromUid == null) {
			model.setUid(uid);
		} else {
			model.setUid(fromUid);
		}
		model.setPage_no(pageNo + "");// (page_size);
		model.setFid("4");
		lunTanPresenter.getInfo(model);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		super.onWindowFocusChanged(hasFocus);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.setMargins(0, getStatusBarHeight(), 0, 0);
		findViewById(R.id.top).setLayoutParams(params);
	}

	/**
	 * 初始化数据
	 */

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {

			if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiSuccess) {
				if ("more".equals(tag)) {
				} else {
					arrayList.clear();
				}
				arrayList.addAll(((TieZiBaoHanReply) entity.getData())
						.getList());
				adapter.notifyDataSetChanged();
			} else if (entity.getEventType() == ConfigAppPresenter.Guanzhu_success) {
				vipPersonalBean = (VIPPersonalBean) entity.getData();
				textName.setText(vipPersonalBean.getNickname());
				textNum.setText(vipPersonalBean.getFocus());
				// textGuanZhu.setText(vipPersonalBean.getFocus());
				texttextFenShiNum.setText(vipPersonalBean.getBeFocus());// 粉丝数
				if (vipPersonalBean.getSign().length() != 0) {
					qianming.setText(vipPersonalBean.getSign());
				} else {
					qianming.setText("还没有签名哦");
				}
				if (fromUid != null) {
					if ("1".equals(vipPersonalBean.getIsFocus())) {
						textGuan.setText("已关注");
					}
				}
				textGuan.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) { // TODO Auto-generated
						if (fromUid != null) {
							presenter.guanzhu(uid, fromUid, "1");
							textGuan.setText("已关注");
						} else {
							ByAlert.alert("自己不可以关注自己哦");
						}
					}
				});
				textSiLiao.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (fromUid != null) {
							Intent intent = new Intent(GeRenActivity.this,
									ActivitySiXinXiangQing.class);
							intent.putExtra("rela_uid", fromUid);
							intent.putExtra("to_nick",
									vipPersonalBean.getNickname());
							Log.e("hu.....3", vipPersonalBean.getNickname()
									+ ",213e4=" + fromUid + ",uid=" + uid);
							startActivity(intent);
						} else {
							ByAlert.alert("不能给自己私信哦");
						}

					}
				});
				Drawable drawable1 = getResources().getDrawable(
						R.drawable.vip001);
				Drawable drawable2 = getResources().getDrawable(
						R.drawable.vip002);
				Drawable drawable3 = getResources().getDrawable(
						R.drawable.vip003);
				drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
				drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
				drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
				if (vipPersonalBean.getVip().getGroup_title().equals("vip2")) {
					// txtbabys.setText("萌娃");
					// imgVip.setBackgroundResource(R.drawable.vip002);
					textName.setCompoundDrawables(null, null, drawable2, null);
					// textVip.setText(vipPersonalBean.getVip().getGroup_title());
				} else if (vipPersonalBean.getVip().getGroup_title()
						.equals("vip1")) {
					// txtbabys.setText("萌娃");
					// imgVip.setBackgroundResource(R.drawable.vip001);
					textName.setCompoundDrawables(null, null, drawable1, null);
					// textVip.setText(vipPersonalBean.getVip().getGroup_title());
				} else {
					// imgVip.setBackgroundResource(R.drawable.vip003);
					textName.setCompoundDrawables(null, null, drawable3, null);
					// textVip.setText("未激活");
				}
				if ("".equals(vipPersonalBean.getHead())
						|| vipPersonalBean.getHead() == null) {
					if ("1".equals(vipPersonalBean.getSex())) {
						roundImage.setImageResource(R.drawable.male);
					} else {
						roundImage.setImageResource(R.drawable.female);
					}

				} else {
					if (vipPersonalBean.getHead().startsWith("http")) {
						XImageLoader
								.load(vipPersonalBean.getHead(), roundImage);
					} else {
						XImageLoader
								.load(GlobalConfig.API_URL
										+ "/picture/index?id="
										+ vipPersonalBean.getHead(), roundImage);
					}

				}

				// if (vipPersonalBean.getId().equals(uid)) {
				// SPUtils.put(this, null, SpPublicParams.USERHEAD,
				// vipPersonalBean.getHead());
				// }
				// qianming.setText(vipPersonalBean.get)
			} else if (entity.getEventType() == ConfigAppPresenter.GuanzhuFailEvent) {
				ByAlert.alert(entity.getData() + "");
			}
		}
		stopLoad();
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		showProgressDialog("正在刷新，请稍后", true);
		tag = "up";
		pageNo = 1;
		model.setPage_no(pageNo + "");// (page_size);
		lunTanPresenter.getInfo(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		showProgressDialog("正在加载，请稍后", true);
		pageNo++;
		tag = "more";
		model.setPage_no(pageNo + "");// (page_size);
		lunTanPresenter.getInfo(model);
	}

	public void stopLoad() {
		listview.stopLoadMore();
		listview.stopRefresh();
	}
}
